package com.suos.core.logic;

import cn.hutool.cache.CacheUtil;
import cn.hutool.core.util.ObjectUtil;
import com.suos.conf.SuOsProperties;
import com.suos.core.entity.Abnormal;
import com.suos.core.entity.SysInfo;
import com.suos.core.utils.PriUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 处理客户端发送的信息
 *
 * @author : MisShi   telnet 127.0.0.1 9090
 * @date: 2021-10-22 15:21:18
 */
@Slf4j
@NoArgsConstructor
@ChannelHandler.Sharable
public class SugarServerHandler extends ChannelInboundHandlerAdapter {


    private SuOsProperties suOsProperties;

    public SugarServerHandler(SuOsProperties suOsProperties) {
        this.suOsProperties = suOsProperties;
    }

    /**
     * order : 1
     *
     * @return boolean
     */
    @Override
    public boolean isSharable() {
        return super.isSharable();
    }

    /**
     * order : 2
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    }

    /**
     * order : 3
     */
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    }

    /**
     * order : 4
     * <p>
     * 数据传输(存放数据)
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
    }

    /**
     * order : 5
     * 读取客户端传输数据
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (ObjectUtil.isNotEmpty(msg)) {
            SysInfo sysInfo = (SysInfo) msg;
            boolean orHave = PriUtil.sysInfos.stream().anyMatch(item -> item.getUnbid().equals(sysInfo.getUnbid()));
            if (orHave) {
                Abnormal abnormal = new Abnormal(sysInfo.getUnbid(), sysInfo.getUnbid() + " 唯一标识重复");
                ctx.writeAndFlush(abnormal);
            } else {
                PriUtil.sysInfos.add(sysInfo);
                ctx.writeAndFlush(PriUtil.sysInfos);
            }
        }
    }

    /**
     * order : 6
     * 数据读取完毕
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    }

    /**
     * order : 7
     * 出现异常
     */

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("服务端传输异常:{}", cause.getMessage());
        ctx.channel().closeFuture();
    }

    /**
     * order : 8
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

    }

    /**
     * order : 9
     */
    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
    }

    /**
     * order : 10
     */
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
    }

    @Override
    protected void ensureNotSharable() {
    }

}
